<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let obj = {
            name: '小明',
            age: 20,
            arr: [1, 2]
        }

        function deepClone(value) {
            // 判断传入参数不是对象或数组时直接返回传入的值，不再执行函数
            if (typeof value !== 'object' || value == null) {
                return value
            }

            //定义函数的返回值
            let result
            // 判断传进来的数据类型数组还是对象，对应创建新的空数组或对象
            if (value instanceof Array) {
                result = []
            } else {
                result = {}
            }

            // 循环遍历拷贝
            for (let key in value) {
                console.log('for',value,'++++++',key,"++++++",value[key]);
                //函数递归实现深层拷贝
                result[key] = deepClone(value[key])
            }

            // 将拷贝的结果返回出去
            return result
        }

        let newObj = deepClone(obj)
        obj.arr[0] = 0 // 修改原对象的arr属性对应的数组的元素值
        console.log("obj", obj)
        console.log("newObj ", newObj)
    </script>
</body>

</html>